import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class csv {

    // 正则表达式，匹配除英文、中文、标点符号、数字以外的字符
    private static final String REGEX = "[^\\p{L}\\p{N}\\p{P}\\p{M}]";

    public static void main(String[] args) {
        String inputCsvFile = "src/test.csv";
        String outputCsvFile = "output.csv";
        try {
            BufferedReader br = new BufferedReader(new FileReader(inputCsvFile));
            BufferedWriter bw = new BufferedWriter(new FileWriter(outputCsvFile));
            String line;
            while ((line = br.readLine()) != null) {
                String[] fields = line.split(",");
                for (int i = 0; i < fields.length; i++) {
                    String cleanedField = cleanString(fields[i]);
                    bw.write(cleanedField);
                    if (i < fields.length - 1) {
                        bw.write(",");
                    }
                }
                bw.newLine();
            }
            br.close();
            bw.close();
            System.out.println("CSV文件处理完成。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 清洗字符串，去除除英文、中文、标点符号、数字以外的字符
    private static String cleanString(String str) {
        Pattern pattern = Pattern.compile(REGEX);
        Matcher matcher = pattern.matcher(str);
        return matcher.replaceAll("").trim();
    }
}
